package com.zegome.game.gomestory.main.scene.play.board;
import android.util.Log;
import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.TweenCallback;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.gomestory.main.scene.BaseGameScreen;
import com.zegome.game.gomestory.main.scene.draw.Draw3Screen;
import com.zegome.game.gomestory.main.scene.play.PlayingScreen;
import com.zegome.game.handler.IUpdateHandler;

public class Board {
		
	public float tickTime;	
	boolean isBot;
	boolean isInVS;
	boolean isInDraw;
	public int combo, pika;
	public BaseGameScreen screen;
	public DataMap dataMap;
	public MoveProcessor moveProcessor;
	public AnimationEatManager animationEat;
	public CheckEat checkEat;
	public SelectProcessor selectProcessor;
	public InputProcessor inputProcessor;
	public GetDataMap getDataMap;
	public EffectArrangeMap effectArrangeMap;
	public SearchEat searchEat;
	public RecCp recCp;
	Clock clock;	
	ImageButton find, swap;
	Label time;
	public boolean isPlaying;
	public BoardImage img;
	boolean isFirst = true;
	LayerMode layerMode;
	DiamondMod diamondMod;
	BgPikachu bgPikachu;
	
	int level;
	
	int chapter;
	
	public Board(BaseGameScreen screen,boolean isInDraw, boolean isInVS, boolean isBot, float wiMap, float hiMap, float xStart, float yStart){		
		this.isBot = isBot;
		this.screen = screen;
		this.isInVS = isInVS;
		dataMap = new DataMap(this, wiMap, hiMap, xStart, yStart);
		animationEat = new AnimationEatManager(this);
		inputProcessor = new InputProcessor(this);
		getDataMap = new GetDataMap(this);
		effectArrangeMap = new EffectArrangeMap(this);
		selectProcessor = new SelectProcessor(this);
		checkEat = new CheckEat(this);
		moveProcessor = new MoveProcessor(this);
		searchEat = new SearchEat(this);		
		img = new BoardImage();
		layerMode = new LayerMode(this, 0, 0);
		recCp = new RecCp(this);
		this.isInDraw = isInDraw;
		diamondMod = new DiamondMod(this);
		bgPikachu = new BgPikachu(this);
	}
	
	public void start(int chapter, String map, int mapRow, int mapCol,int time, int numberPika,int stylePlay){				
		this.chapter = chapter;
		img.load2(this,chapter);
		if(isBot == false){
			if(bg == null){
				bg = new Sprite(0, 0,img.bg);
				bg.setSize((800), (480));
				bg.setZIndex(-1);		
				screen.attachChild(bg);
				screen.addIconGomeSmall();
			}
			bg.setTextureRegion(img.bg);
			bg.setSize((800), (480));			
		}		
		getDataMap.set(map, mapRow, mapCol, time, numberPika, stylePlay);
		start2();
	}
	
	
	public void start(int level){		
		this.level = level;
		this.chapter = screen.ctr.d.getChapter(level);
		img.load(this, level);
		if(isBot == false){
			if(bg == null){
				bg = new Sprite(0, 0,img.bg);
				bg.setSize((800), (480));
				bg.setZIndex(-1);		
				screen.attachChild(bg);
				screen.addIconGomeSmall();
			}
			bg.setTextureRegion(img.bg);
			bg.setSize((800), (480));		
		}			
		getDataMap.loadLevel(level);
		start2();
	}
	
	
	int tempMinute, tempSecond;
	Sprite bg;
	
	public void start2(){
		recCp.reset();
		
		if(isBot == false){			
			if(chapter == 7){
				screen.ctr.a.bgSong.play();
			}
			else{
				screen.ctr.a.mainSong.play();
			}
		}
		
		moveProcessor.reset();
		selectProcessor.reset();
		isPlaying = true;
		dataMap.time=dataMap.time/1000;
		
		combo = 0;
		pika = 0;
		
		if(isBot == false){
		if(isFirst==true){
		if(find == null){		
			find = screen.ctr.stageHelper.createImageButton(screen.ctr.g.mSkin, screen.ctr.a.find, null, null, (5), (480-407),70,70);
			
			find.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {					
					if(screen.ctr.a.isHasDialog() == true)
						return;				
					if(screen.ctr.d.money<50){
						screen.ctr.androidHelper.showToast(screen.ctr.lang.getNotEnoughMoney(1, 50));
						return;
					}
					else{
						screen.ctr.d.money-=50;
						screen.ctr.d.saveAccount();
						screen.ctr.androidHelper.showToast(screen.ctr.lang.getSubMoney(1, 50));
					}
//					AniGift a = new AniGift(Board.this);
//					a.in();
//					return;
					for(int row=0;row<10;row++){
						for(int col=0;col<14;col++){
							dataMap.map[row][col].setSelect(false);
						}
					}
					int[] data = searchEat.search();
					if(data != null){
						dataMap.map[data[0]][data[1]].setSelect(true);
						dataMap.map[data[2]][data[3]].setSelect(true);
					}
				}
			});
		}
		screen.getStage().addActor(find);
		if(swap == null){
			swap = screen.ctr.stageHelper.createImageButton(screen.ctr.g.mSkin, screen.ctr.a.swap, null, null, (5), (480-306-10),70,70);
			swap.addListener(new ChangeListener() {
				@Override
				public void changed(ChangeEvent event, Actor actor) {					
					if(screen.ctr.a.isHasDialog() == true)
						return;			
					if(screen.ctr.d.money<50){
						screen.ctr.androidHelper.showToast(screen.ctr.lang.getNotEnoughMoney(1, 50));
						return;
					}
					else{
						screen.ctr.d.money-=50;
						screen.ctr.d.saveAccount();
						screen.ctr.androidHelper.showToast(screen.ctr.lang.getSubMoney(1, 50));
					}
					effectArrangeMap.changeMap();
				}
			});
			
		}
		screen.getStage().addActor(swap);
		if(clock == null){
			clock = new Clock(this, (6), (480-201));
			clock.setSize((97), (97));
			screen.attachChild(clock);
		}
		clock.scale = 0.0f;
		if(time == null){
			//time = screen.ctr.stageHelper.createLabel(screen.ctr.g.mFont, dataMap.time+"", Color.WHITE, (20), (480-228));			
			time = screen.ctr.stageHelper.createLabel(screen.ctr.g.mFont, "", Color.WHITE, 0, 0);
			time.setText(dataMap.time+"");
			time.setPosition((20), (480-215));
		}
		screen.getStage().addActor(time);
		}
		}
		animationEat.init();
		if(isBot == false)
			if(isFirst == true)
				inputProcessor.init();
		effectArrangeMap.start();
		tickTime = 0;
		if(isFirst == true){
		screen.registerUpdateHandler(new IUpdateHandler() {
			
			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onUpdate(float delta) {
				// TODO Auto-generated method stub				
				if(screen.state!=1)
					return;
				if(isPlaying){
					
				if(isBot == false){
					if(screen.ctr.a.isHasDialog() == true && isInVS == false)
						return;
				recCp.update(delta);
				tickTime+=delta;
				tempMinute = (int) ((dataMap.time - tickTime)/60);
				tempSecond = (int) ((dataMap.time - tickTime) - tempMinute*60);
				time.setText(tempMinute+":"+tempSecond);
				clock.scale = 1-(float)tickTime/(float)(dataMap.time);

				if(tempSecond == 0&&tempMinute == 0){
				if(isInVS == false){
					isPlaying = false;
					screen.ctr.a.bgSong.pause();
				screen.ctr.a.mainSong.pause();
			ChangeListener menuLis = new ChangeListener() {						
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					screen.fadeOut(new TweenCallback() {								
						@Override
						public void onEvent(int arg0, BaseTween<?> arg1) {
							// TODO Auto-generated method stub
							screen.ctr.a.bgSong.pause();
							screen.ctr.a.mainSong.pause();									
							screen.ctr.a.clearMusic2();
							screen.ctr.a.clearGame();									
							BaseGameScreen screen = Board.this.screen;
							if(isInDraw){
								clear();
								((Draw3Screen)(screen)).board = null;										
								screen.ctr.a.loadedDraw();										
								screen.getController().setScreen(screen.getController().getScreen("DrawScreen"));
							}
							else{
								clear();
								((PlayingScreen)(screen)).board = null;
								screen.ctr.a.loadedLevel();
								screen.getController().setScreen(screen.getController().getScreen("LevelScreen"));														
							}
						}
					});
				}
			};
			
			ChangeListener replayLis = new ChangeListener() {						
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					if(isInDraw){
						start(screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).chapter,screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).map, screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).mapRow, screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).mapCol, screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).time, screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).numberPika, screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).stylePlay);
					}
					else
						start(PlayingScreen.GAME_LEVEL);
				}
			};
			
			screen.ctr.stageHelper.createDialogLose(screen.getStage(), screen.getTween(), pika, combo, menuLis, replayLis);
				}
				}
				}
			}}

			@Override
			public boolean isDetachSelf() {
				// TODO Auto-generated method stub
				return false;
			}
		});
		if(isBot == true){			
			screen.registerUpdateHandler(new IUpdateHandler() {
				
				@Override
				public void reset() {
					// TODO Auto-generated method stub					
				}
				
				@Override
				public void onUpdate(float delta) {
					// TODO Auto-generated method stub
					
					if(screen.state != 1)
						return;
					if(timeChoice == -1)
						timeChoice = 3;
					if(isPlaying){
					if(tickTime>=timeChoice){						
						tickTime = 0;
						int[] data = searchEat.search();
						if(data == null){
							if(dataMap.total>0)
								effectArrangeMap.changeMap();
						}
						else{
							selectProcessor.click(dataMap.map[data[0]][data[1]]);
							selectProcessor.click(dataMap.map[data[2]][data[3]]);
						}
						//timeChoice = (dataMap.total/4+1)*(0.4f+1*((dataMap.stylePlay!=4)?0.01f:0.0f));
						timeChoice = screen.ctr.d.startB + dataMap.total/6*(screen.ctr.d.factorB + 0.01f * (dataMap.stylePlay==4?0:1));  
						//Log.i("VSSCREEN",dataMap.total +"-----"+timeChoice);
					}
					else{
						tickTime+=delta;
					}
					}
				}

				@Override
				public boolean isDetachSelf() {
					// TODO Auto-generated method stub
					return false;
				}
			});
		}
		isFirst = false;
		}
		
		if(isInDraw == false && isInVS == false){
		if( level== 1){
			screen.ctr.stageHelper.createDialog(screen.getStage(), screen.getTween(), screen.ctr.lang.getTextStartGame(1), 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub				
				}
			}, null);
		}
		else if(level == 21){
			screen.ctr.stageHelper.createDialog(screen.getStage(), screen.getTween(), screen.ctr.lang.getTextStartGame(21), 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					
				}
			}, null);
		}
		else if(level == 41){
			screen.ctr.stageHelper.createDialog(screen.getStage(), screen.getTween(), screen.ctr.lang.getTextStartGame(41), 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					
				}
			}, null);
		}
		else if(level == 61){
			screen.ctr.stageHelper.createDialog(screen.getStage(), screen.getTween(), screen.ctr.lang.getTextStartGame(61), 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					
				}
			}, null);
		}
		else if(level == 81){
			screen.ctr.stageHelper.createDialog(screen.getStage(), screen.getTween(), screen.ctr.lang.getTextStartGame(81), 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					
				}
			}, null);
		}
		else if(level == 101){
			screen.ctr.stageHelper.createDialog(screen.getStage(), screen.getTween(), screen.ctr.lang.getTextStartGame(101), 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					
				}
			}, null);
		}
		else if(level == 121){
			screen.ctr.stageHelper.createDialog(screen.getStage(), screen.getTween(), screen.ctr.lang.getTextStartGame(121), 1, new ChangeListener() {				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					
				}
			}, null);
		}		
		}
	}
	float timeChoice = -1;
	public void clear(){
		screen.clearUpdateHandler();
		if(animationEat!=null)
		animationEat.clear();		
		if(clock!=null)
			screen.removeChild(clock);
		if(dataMap!=null)
		dataMap.clear();		
		clock = null;
		screen.removeChild(bg);
		bg = null;
		screen.removeChild(layerMode);
		find = null;
		swap = null;	
		dataMap = null;
		moveProcessor = null;
		animationEat = null;
		checkEat = null;
		selectProcessor = null;
		inputProcessor = null;
		getDataMap = null;
		effectArrangeMap = null;
		searchEat = null;
		time = null;
		img = null;
		layerMode = null;
		diamondMod = null;
		screen.removeChild(bgPikachu);
		bgPikachu = null;
	}
}
